package com.errol.batchprocessor.employeeaddition.advisorimpl.payroll;

import com.renjia.dao.*;
import com.renjia.entity.BusPayrollRecord;
import com.renjia.entity.BusPayrollRecordDetail;
import com.renjia.entity.CoreEmployeeImportBatch;
import com.renjia.properties.UkerServerProperties;
import com.renjia.service.CoreEmployeeImportBatchService;
import com.renjia.service.generic.log.LogService;
import com.renjia.service.generic.middleware.batchprocessor.payload.AbstractBatchPayload;
import com.renjia.service.generic.support.customer.CustomerChannelSupportService;
import com.renjia.service.generic.support.employee.BankCardSupportService;
import com.renjia.service.generic.support.employee.EmployeeCustomerSupportService;
import com.renjia.service.generic.support.employee.EmployeeSupportService;
import com.renjia.service.generic.support.payroll.PayrollDetailSupportService;
import com.renjia.service.generic.support.payroll.PayrollRecordSupportService;
import com.renjia.service.integration.threeelements.ThreeElementsService;
import com.renjia.service.module.employee.AgreementSigningService;
import com.renjia.service.module.employeeaddition.advisorimpl.EmployeeAdvisorTypeEnum;
import com.renjia.service.module.employeeaddition.advisorimpl.payroll.payload.PayrollEmployeeBatchPayload;
import com.renjia.service.module.employeeaddition.advisorimpl.payroll.payload.PayrollEmployeeSinglePayload;
import com.renjia.service.module.messagecenter.MsgCenterSendService;
import com.renjia.service.module.payroll.advisor.verify.*;
import com.renjia.util.EpayCallingService;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 费用发放压力测试扩展
 *
 * @author zhang xiao bin
 * @version v1.0
 * @date 2021/03/07
 * @since v5.0.6
 **/
@Service
public class TestPayrollEmployeeAdvisorImpl extends PayrollEmployeeAdvisorImpl {

    public TestPayrollEmployeeAdvisorImpl(CoreEmployeeImportBatchService coreEmployeeImportBatchService,
                                          CoreEmployeeMapper coreEmployeeMapper, CoreEmployeeCustomerMapper coreEmployeeCustomerMapper,
                                          CoreUserBankMapper coreUserBankMapper, CoreAgreementDetailMapper coreAgreementDetailMapper,
                                          ThreeElementsService threeElementsService,
                                          EmployeeSupportService employeeSupportService, EmployeeCustomerSupportService employeeCustomerSupportService,
                                          BankCardSupportService bankCardSupportService, CustomerChannelSupportService customerChannelSupportService,
                                          AgreementSigningService agreementSigningService, MsgCenterSendService msgCenterSendService, EpayCallingService epayCallingService, LogService logService,
                                          PayrollRecordSupportService payrollRecordSupportService, PayrollDetailSupportService payrollDetailSupportService,
                                          RedisTemplate<String, Object> redisTemplate,
                                          BusPayrollRecordMapper busPayrollRecordMapper, BusPayrollRecordDetailMapper busPayrollRecordDetailMapper,
                                          PayrollVerifyBatchStartAdvisor payrollVerifyBatchStartAdvisor,
                                          PayrollVerifySingleValidateAdvisor payrollVerifySingleValidateAdvisor,
                                          PayrollVerifySingleValidateEmployeeAdvisor payrollVerifySingleValidateEmployeeAdvisor,
                                          PayrollVerifySingleProcessAdvisor payrollVerifySingleProcessAdvisor,
                                          PayrollVerifySingleSuccessAdvisor payrollVerifySingleSuccessAdvisor,
                                          PayrollVerifySingleFailedAdvisor payrollVerifySingleFailedAdvisor,
                                          PayrollVerifySinglePostAdvisor payrollVerifySinglePostAdvisor,
                                          PayrollVerifyPiecePostAdvisor payrollVerifyPiecePostAdvisor,
                                          UkerServerProperties ukerServerProperties) {
        super(coreEmployeeImportBatchService, coreEmployeeMapper, coreEmployeeCustomerMapper, coreUserBankMapper, coreAgreementDetailMapper,
                threeElementsService, employeeSupportService, employeeCustomerSupportService, bankCardSupportService, customerChannelSupportService,
                agreementSigningService, epayCallingService, logService, payrollRecordSupportService, payrollDetailSupportService, redisTemplate, busPayrollRecordMapper,
                busPayrollRecordDetailMapper, payrollVerifyBatchStartAdvisor, payrollVerifySingleValidateAdvisor, payrollVerifySingleValidateEmployeeAdvisor,
                payrollVerifySingleProcessAdvisor, payrollVerifySingleSuccessAdvisor, payrollVerifySingleFailedAdvisor, payrollVerifySinglePostAdvisor,
                payrollVerifyPiecePostAdvisor, msgCenterSendService, ukerServerProperties);
    }

    @Override
    public boolean shallProcess(@NonNull AbstractBatchPayload<CoreEmployeeImportBatch, EmployeeAdvisorTypeEnum, BusPayrollRecord, BusPayrollRecordDetail> advisorPayload) {
        return EmployeeAdvisorTypeEnum.PAYROLL_RECORD_TEST == advisorPayload.getAdvisorTypeEnum() && !ukerServerProperties.isProd();
    }

    @Override
    public boolean doPrepareOnBatchStart(@NonNull PayrollEmployeeBatchPayload batchPayload) {
        logService.warn("人员导入批处理：费用发放压测，batchNo=" + batchPayload.getBatchNo());
        return super.doPrepareOnBatchStart(batchPayload);
    }

    @Override
    protected boolean isThreeElementSuccess(PayrollEmployeeSinglePayload singlePayload, PayrollEmployeeBatchPayload batchPayload) {
        if (singlePayload.getIsThreeElementSuccess() != null) {
            return singlePayload.getIsThreeElementSuccess();
        }
        try {
            Thread.sleep(RandomUtils.nextInt(400, 600));
        } catch (InterruptedException e) {
            e.printStackTrace();
            Thread.currentThread().interrupt();
        }
        if (new BigDecimal("0.01").compareTo(singlePayload.getSingleEntity().getInfactPayAmount()) == 0) {
            singlePayload.setThreeElementSuccess(true);
            return true;
        } else {
            singlePayload.setThreeElementSuccess(false);
            singlePayload.getErrMsgList().add(singlePayload.getEmployeeName() + ", 姓名、身份证、银行卡验证失败");
            return false;
        }
    }

    @Override
    protected boolean isDebitBankCard(PayrollEmployeeSinglePayload singlePayload, PayrollEmployeeBatchPayload batchPayload) {
        try {
            Thread.sleep(RandomUtils.nextInt(200, 500));
        } catch (InterruptedException e) {
            e.printStackTrace();
            Thread.currentThread().interrupt();
        }
        if (new BigDecimal("0.01").compareTo(singlePayload.getSingleEntity().getInfactPayAmount()) == 0) {
            singlePayload.setThreeElementSuccess(true);
            return true;
        } else {
            singlePayload.setThreeElementSuccess(false);
            singlePayload.getErrMsgList().add("银行卡不是借记卡");
            return false;
        }
    }
}
